package action;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.OrderService;
import service.ServiceManager;
import form.OrderRegistrationForm;

public class OrderRegistrationAction extends ConnectionAwareAction {
	
	private static final Logger logger = Logger.getLogger(OrderRegistrationAction.class);
	private static final String ORDERREGISTRATIONACTION_INFO = "OrderRegistration is error"; 
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
            throws Exception {
    	
    	OrderRegistrationForm input = (OrderRegistrationForm) form;
    	
        try {
            HttpSession session = request.getSession(true);
            UUID id_ticket = UUID.randomUUID();
            int id_routs = (int) session.getAttribute("id_routs");
            String your_train = input.getYour_train();
            
            String your_rout_from = session.getAttribute("searchFrom")+ ""; 
            String your_rout_to = session.getAttribute("searchTo")+"";
            String date_routs = session.getAttribute("date_routs")+ "";
            
            int cityFrom = (int) session.getAttribute("cityFrom");
            int cityTo = (int) session.getAttribute("cityTo");
            
            session.removeAttribute("id_routs");
            session.removeAttribute("searchFrom");
            session.removeAttribute("searchTo");
            session.removeAttribute("date_touts");
            session.removeAttribute("cityFrom");
            session.removeAttribute("cityTo");
            session.invalidate(); 
            
            String first_name = Escape.escapeHTML(input.getFirst_name());
            String last_name = Escape.escapeHTML(input.getLast_name());
            String midle_name = Escape.escapeHTML(input.getMidle_name());
            
            String names = last_name+" "+first_name +" "+midle_name;
            
            OrderService orderService = ServiceManager.getDefault().getOrderService();
           
            if (cityFrom > 0 && cityTo > 0 && date_routs!= null && names != null) {
            	orderService.setOrder(cityFrom, cityTo, id_routs, names, date_routs,id_ticket);
            	request.setAttribute("name_order",names);
            	request.setAttribute("cityFrom", your_rout_from);
            	request.setAttribute("cityTo", your_rout_to);
            	request.setAttribute("your_train", your_train);
            	request.setAttribute("id_ticket",id_ticket);
            	return mapping.findForward(ForwardActionTo.SUCCESS);
            } else {
            	return mapping.findForward(ForwardActionTo.ERROR);
            }
        } catch (Exception e) {
        	logger.error(OrderRegistrationAction.ORDERREGISTRATIONACTION_INFO + e.getMessage());
        } 
    	return null;
    }
}
